package com.quanyan.travel.service.impl;

import com.quanyan.messagepush.enums.IdType;
import com.quanyan.messagepush.enums.MsgType;
import com.quanyan.travel.component.pushMsg.MsgPush;
import com.quanyan.travel.component.pushMsg.TravelMsgModel;
import com.quanyan.travel.constants.OutdoorConstants;
import com.quanyan.travel.entity.TbCommonParameter;
import com.quanyan.travel.service.TravelBaseService;
import com.quanyan.travel.service.TravelMsgService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单Service
 * Created by yangyun on 2016/4/19.
 */


@Service("travelMsgService")
public class TravelMsgServiceImpl implements TravelMsgService {

    private static Logger logger = Logger.getLogger(TravelMsgServiceImpl.class);

    @Autowired
    private MsgPush msgPush;

    @Autowired
    private TravelBaseService travelBaseService;


    @Override
    public void refundSuccessMsg(Integer uid, String orderNo) {
        TbCommonParameter tbGameCommonParameter = travelBaseService.getTbCommonParameter(MsgPush.USER_REFUND_SUCCESS_MSG);
        TravelMsgModel travelMsgModel = new TravelMsgModel();
        String msg = MsgPush.msgUtils(tbGameCommonParameter.getParameterValue(), orderNo);
        travelMsgModel.setAlertTitle(OutdoorConstants.DEFAULT_APP_NAME);
        travelMsgModel.setAlertBody(OutdoorConstants.DEFAULT_APP_TRAVEL_NAME+msg);
        travelMsgModel.setMsgType(MsgType.NOTIFY_MSG);
        travelMsgModel.setMsgTitle(msg);
        List<Integer> uids = new ArrayList<>();
        uids.add(uid);
        travelMsgModel.setToUidList(uids);
        msgPush.pushMessageOperate(travelMsgModel);
        logger.info("用户报名成功,推送消息,当前订单号ID:"+orderNo);
    }

    @Override
    public void preRefundMsg(Integer uid, Map<String, Object> params) {
        TbCommonParameter tbGameCommonParameter = travelBaseService.getTbCommonParameter(MsgPush.USER_SUBMIT_REFUND_MSG);
        String body = MsgPush.msgUtils(tbGameCommonParameter.getParameterValue(), params.get("orderNo") + "");
        TravelMsgModel travelMsgModel = new TravelMsgModel();
        travelMsgModel.setAlertTitle(OutdoorConstants.DEFAULT_APP_NAME);
        travelMsgModel.setAlertBody(OutdoorConstants.DEFAULT_APP_TRAVEL_NAME+body);
        travelMsgModel.setMsgType(MsgType.JUMP_MSG);
        travelMsgModel.setIdType(IdType.TRAVEL_ORDER_DETAIL_PAGE);
        travelMsgModel.setParams(params);
        travelMsgModel.setMsgTitle(body);
        List<Integer> uids = new ArrayList<>();
        uids.add(uid);
        travelMsgModel.setToUidList(uids);
        msgPush.pushMessageOperate(travelMsgModel);
        logger.info("用户提交退款,推送消息,当前订单号ID:"+ params.get("orderNo")+"");
    }

    @Override
    public void paySuccessMsg(Integer uid, String activityName, BigDecimal price, Long startTime,  Map<String, Object> params) {
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd"); //格式化当前系统日期
        String dateTime = dateFm.format(startTime);
        TbCommonParameter tbGameCommonParameter = travelBaseService.getTbCommonParameter(MsgPush.USER_PAY_SUCCESS_MSG);
        String body = MsgPush.msgUtils(tbGameCommonParameter.getParameterValue(), activityName, price + "", dateTime);
        TravelMsgModel travelMsgModel = new TravelMsgModel();
        travelMsgModel.setAlertTitle(OutdoorConstants.DEFAULT_APP_NAME);
        travelMsgModel.setAlertBody(OutdoorConstants.DEFAULT_APP_TRAVEL_NAME+body);
        travelMsgModel.setMsgType(MsgType.JUMP_MSG);
        travelMsgModel.setIdType(IdType.TRAVEL_ORDER_DETAIL_PAGE);
        travelMsgModel.setParams(params);
        travelMsgModel.setMsgTitle(body);
        List<Integer> uids = new ArrayList<>();
        uids.add(uid);
        travelMsgModel.setToUidList(uids);
        msgPush.pushMessageOperate(travelMsgModel);
        logger.info("用户报名成功,推送消息,当前活动:"+activityName);
    }

    @Override
    public void payRemindMsg(Integer uid, Map<String, Object> params, String activityName) {
        TbCommonParameter tbGameCommonParameter = travelBaseService.getTbCommonParameter(MsgPush.USER_ACTIVITY_WAIT_PAY);
        String msg = MsgPush.msgUtils(tbGameCommonParameter.getParameterValue(), activityName);
        TravelMsgModel travelMsgModel = new TravelMsgModel();
        travelMsgModel.setAlertTitle(OutdoorConstants.DEFAULT_APP_NAME);
        travelMsgModel.setAlertBody(OutdoorConstants.DEFAULT_APP_TRAVEL_NAME+msg);
        travelMsgModel.setMsgType(MsgType.JUMP_MSG);
        travelMsgModel.setIdType(IdType.TRAVEL_ORDER_DETAIL_PAGE);
        travelMsgModel.setParams(params);
        travelMsgModel.setMsgTitle(msg);
        List<Integer> uids = new ArrayList<>();
        uids.add(uid);
        travelMsgModel.setToUidList(uids);
        msgPush.pushMessageOperate(travelMsgModel);
        logger.info("支付提醒,推送消息,活动名称:"+activityName);
    }

    @Override
    public void userFavActivityPublishMsg(List<Integer> uid, Map<String, Object> params, String activityName) {
        TbCommonParameter tbGameCommonParameter = travelBaseService.getTbCommonParameter(MsgPush.USER_FAVORITE_ACTIVITY_PUBLISH_MSG);
        String msg = MsgPush.msgUtils(tbGameCommonParameter.getParameterValue(), activityName);
        TravelMsgModel travelMsgModel = new TravelMsgModel();
        travelMsgModel.setAlertTitle(OutdoorConstants.DEFAULT_APP_NAME);
        travelMsgModel.setAlertBody(OutdoorConstants.DEFAULT_APP_TRAVEL_NAME+msg);
        travelMsgModel.setMsgType(MsgType.JUMP_MSG);
        travelMsgModel.setIdType(IdType.TRAVEL_ACTIVITY_PAGE);
        travelMsgModel.setParams(params);
        travelMsgModel.setToUidList(uid);
        travelMsgModel.setMsgTitle(msg);
        msgPush.pushMessageOperate(travelMsgModel);
        logger.info("用户喜欢的活动发布,推送消息,活动名称:"+activityName);
    }

    @Override
    public void exceptionOrderMsg(Integer uid, String orderNo) {
        TbCommonParameter tbGameCommonParameter = travelBaseService.getTbCommonParameter(MsgPush.EXCEPTION_ORDER_MSG);
        TravelMsgModel travelMsgModel = new TravelMsgModel();
        String msg = MsgPush.msgUtils(tbGameCommonParameter.getParameterValue(), orderNo);
        travelMsgModel.setAlertTitle(OutdoorConstants.DEFAULT_APP_NAME);
        travelMsgModel.setAlertBody(OutdoorConstants.DEFAULT_APP_TRAVEL_NAME+msg);
        travelMsgModel.setMsgType(MsgType.NOTIFY_MSG);
        List<Integer> uids = new ArrayList<>();
        uids.add(uid);
        travelMsgModel.setMsgTitle(msg);
        travelMsgModel.setToUidList(uids);
        msgPush.pushMessageOperate(travelMsgModel);
        logger.info("用户订单出现异常,当前订单号ID:"+orderNo);
    }
}
